const context = require("koa/lib/context");
const connection = require("../app/database");
class CapitalService {
  async create(item) {
    try {
      const { type, ddescribe, income, expend, cash, remark, id } = item;
      const statement = `INSERT INTO capital (type, ddescribe, income, expend, cash, remark, user_id) VALUES (?,?,?,?,?,?,?);`;
      const result = await connection.execute(statement, [
        type,
        ddescribe,
        income,
        expend,
        cash,
        remark,
        id
      ]);
      return result;
    } catch (error) {
      console.log(error);
    }
  }
  async getAllCapital() {
    const statement = `SELECT * FROM capital;`;
    const result = await connection.execute(statement);
    return result[0];
  }
  async getCapital(id) {
    const statement = `SELECT * FROM capital WHERE id = ?;`;
    const result = await connection.execute(statement, [id]);
    return result[0];
  }
  async updateCapital(item) {
    try {
      const { type, ddescribe, income, expend, cash, remark, id } = item;
      const statment = `UPDATE capital SET type=?,ddescribe=?,income=?,expend=?,cash=?,remark=? WHERE id = ?;`;
      const result = await connection.execute(statment, [
        type,
        ddescribe,
        income,
        expend,
        cash,
        remark,
        id
      ]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }
  async deleteCapital(id) {
    const statement = `DELETE FROM capital WHERE id = ?;`;
    const result = await connection.execute(statement, [id]);
    return result[0];
  }
}

module.exports = new CapitalService();
